﻿using System;
using System.Windows.Input;
using VMDS.Business.Entities;
using VMDS.GUIClient.Helper;
using VMDS.GUIClient.Model;

namespace VMDS.GUIClient.ViewModels
{
    using System.Collections.Generic;

    internal class MainWindowViewModel : BaseViewModel
    {
        private  string _taskLibButText;
        private  string _admLibButText;
        private bool _admAvailable;
        private string _link;

       

        public MainWindowViewModel()
        {
            _admLibButText = "Управление"; 
            var i  = OperationsViewModel.Instance.GetCount();
          _taskLibButText = "Текущие операции ("+i+")";
            NavigationLink = CrossWindowConnector.Pages.VmLibrary;
            CrossWindowConnector.MainView = this;
            _admAvailable = Repozitory.AdmAvailable;
        }

        public string NavigationLink
        {
            get { return _link; }
            set
            {
                _link = value;
                OnPropertyChanged(() => NavigationLink);
                changeselect((_link));
            }
        }

        public string AdmButtonText
        {
            get { return _admLibButText; }
            set { _admLibButText = value;
                OnPropertyChanged(() => AdmButtonText);
            }
        }

        public ICommand VmSelect
        {
            get { return new BaseCommand(Reflesh); }
        }

        void Reflesh()
        {
            var mv = (VmLibraryViewModel)CrossWindowConnector.VmLib;
            try
            {
                Repozitory.OnlineVms = new List<VM>(ServiceLocator.MainService.GetVMs());
               
            }
            catch (Exception ex)
            {
                Repozitory.AdmAvailable = false;
                var msg = ex.Message;
            }
            Repozitory.OfflineVms = ServiceLocator.ClientService.GetOfflineVMs();
            mv.ReloadVms();
        }

        private bool _vm_sel;
        public bool VmSelected
        {
            get { return _vm_sel; }
            set { _vm_sel = value;OnPropertyChanged(()=>VmSelected); }
        }

        public string TaskLibButtonText
        {
            get { return _taskLibButText; }
            set
            {
                _taskLibButText = value; OnPropertyChanged(() => TaskLibButtonText); }
        }

        public ICommand TaskSelect
        {
            get { return new BaseCommand(() => { NavigationLink = CrossWindowConnector.Pages.Operations; }); }
        }

        private bool _task_sel;
        public bool TaskSelected
        {
            get { return _task_sel; }
            set { _task_sel = value; OnPropertyChanged(() => TaskSelected); }
        }

        

        public bool AdmAvailable
        {
            get { return _admAvailable; }
        }

        public ICommand AdmSelect
        {
            get { return new BaseCommand(() =>
                                             {
                                                 if (AdmSelected)
                                                 {
                                                     NavigationLink = CrossWindowConnector.Pages.VmLibrary;
                                                     this.Reflesh();
                                                 }
                                                 else
                                                 {
                                                     NavigationLink = CrossWindowConnector.Pages.Management;
                                                 }
                                             }); 
            }
        }

        private bool _adm_sel;
        public bool AdmSelected
        {
            get { return _adm_sel; }
            set
            {
                _adm_sel = value;
                AdmButtonText = value ? "Закончить управление" : "Управление";
                OnPropertyChanged(() => AdmSelected);
            }
        }

        void changeselect(string val)
        {
            VmSelected = (val == CrossWindowConnector.Pages.VmLibrary);
            TaskSelected = (val == CrossWindowConnector.Pages.Operations);
            AdmSelected = (val == CrossWindowConnector.Pages.Management);
        }

    }
}